<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: paypal.php 86 2013-09-21 12:03:42Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 86 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 12:03:42 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
require('includes/application_top.php');
require('../includes/classes/class.paypal_checkout.php');
require('includes/classes/class.paypal.php');

$paypal = new paypal_admin();

switch($_GET['view']) {
	case 'refund' :
		if(isset($_GET['paypal_ipn_id']))
			$ipn_data = $db->db_query("SELECT * FROM ".TABLE_PAYPAL." WHERE paypal_ipn_id = '".(int) $_GET['paypal_ipn_id']."'");
		
		if($_GET['action'] == 'perform') {
			$txn_id = $_POST['txn_id'];
			$ipn_id = $_POST['ipn_id'];
			$amount = $_POST['amount'];
			$note = $_POST['refund_info'];
			$refund_amount = $_POST['refund_amount'];
			$ipn_data = $db->db_query("SELECT * FROM ".TABLE_PAYPAL." WHERE paypal_ipn_id = '".(int) $ipn_id."'");
			$response = $paypal->RefundTransaction($txn_id, $ipn_data->fields['mc_currency'], $amount, $refund_amount, $note);
			
			if($response['ACK'] == 'Success')
				redirect(href_link(FILENAME_PAYPAL, 'err=refund_Success'));
			else
				redirect(href_link(FILENAME_PAYPAL, 'view=detail&paypal_ipn_id='.(int) $ipn_id.'&err=error_'.$response['L_ERRORCODE0']));
		}
		break;
	case 'search' :
		$date = array();
		$date['actual']['tt'] = date('d');
		$date['actual']['mm'] = date('m');
		$date['actual']['yyyy'] = date('Y');
		$last_month  = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
		$date['last_month']['tt'] = date('d',$last_month);
		$date['last_month']['mm'] = date('m',$last_month);
		$date['last_month']['yyyy'] = date('Y',$last_month);
		if($_GET['action'] == 'perform') 
			$response = $paypal->TransactionSearch($_POST);
		break;
	case 'capture' :
		if(PAYPAL_COUNTRY_MODE!='uk') redirect(href_link(FILENAME_PAYPAL));
		if(isset($_GET['paypal_ipn_id'])) 
			$ipn_data = $db->db_query("SELECT * FROM ".TABLE_PAYPAL." WHERE paypal_ipn_id = '".(int) $_GET['paypal_ipn_id']."'");
		
		if($_GET['action'] == 'perform') {
			$txn_id = $_POST['txn_id'];
			$ipn_id = $_POST['ipn_id'];
			$amount = $_POST['amount'];
			$note = $_POST['refund_info'];
			$capture_amount = $_POST['capture_amount'];
			$ipn_data = $db->db_query("SELECT * FROM ".TABLE_PAYPAL." WHERE paypal_ipn_id = '".(int) $ipn_id."'");
			$response = $paypal->DoCapture($txn_id, $ipn_data->fields['mc_currency'], $amount, $capture_amount, $note);
			if($response['ACK'] == 'Success') {
				$response = $paypal->GetTransactionDetails($ipn_data->fields['txn_id']);
				$data = array();
				$data['paypal_ipn_id'] = $ipn_id;
				$data['txn_id'] = $txn_id;
				$data['payment_status'] ='Pending';
				$data['pending_reason'] = 'partial-capture';
				$data['mc_amount'] = $capture_amount;
				$data['date_added']='now()';
				if($response['PAYMENTSTATUS']=='Completed') {
					$data['payment_status'] = 'Completed';
					$data['pending_reason'] = 'completed-capture';
					$db->db_query("UPDATE ".TABLE_PAYPAL." SET payment_status='Completed',pending_reason='',mc_gross=mc_authorization WHERE paypal_ipn_id='".$ipn_id."'");
				}
				$db->db_query("UPDATE ".TABLE_PAYPAL." SET mc_captured = (mc_captured+".$capture_amount.") WHERE paypal_ipn_id='".$ipn_id."'");
				$db->db_perform('paypal_status_history',$data);
				// update transaction
				redirect(href_link(FILENAME_PAYPAL, 'err=capture_Success'));
			} else 
				redirect(href_link(FILENAME_PAYPAL, 'view=capture&paypal_ipn_id='.(int) $ipn_id.'&err=error_'.$response['L_ERRORCODE0']));
		}
	break;
}
require(DIR_WS_INCLUDES.'metatag.php');
?>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php'); ?>
<div id="wrapper">
<table class="outerTable" cellpadding="0" cellspacing="0">
	<tr>
		<td width="<?php echo BOX_WIDTH; ?>" valign="top">
			<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
		</td>
		<td class="boxCenter" valign="top">
			<table border="0" width="100%" cellspacing="0" cellpadding="2">
				<tr>
					<td width="100%">
						<table border="0" class="table_pageHeading" width="100%" cellspacing="0" cellpadding="0">
							<tr>
								<td width="100" rowspan="2"><img src="https://www.paypal.com/de_DE/DE/i/logo/logo_110x35.gif"></td>
								<td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
								<?php if(!isset($_GET['view'])) { ?>
									<td valign="middle" align="right"><a class="button" href="<?php echo href_link(FILENAME_PAYPAL, 'view=search'); ?>"><?php echo BUTTON_SEARCH; ?></a></td>
								<?php } ?>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td>
					<?php
					if(isset($_GET['err']))
						$error = $paypal->getErrorDescription($_GET['err']);
					switch($_GET['view']) {
						case 'detail' :
							include(DIR_WS_MODULES.'paypal_transactiondetail.php');
							break;
						case 'refund' :
							include(DIR_WS_MODULES.'paypal_refundtransaction.php');
							break;
						case 'capture' :
							include(DIR_WS_MODULES.'paypal_capturetransaction.php');
							break;
						case 'search' :
							include(DIR_WS_MODULES.'paypal_searchtransaction.php');
							break;
						case 'auth' :
							include(DIR_WS_MODULES.'paypal_authtransaction.php');
							break;
						default :
							include(DIR_WS_MODULES.'paypal_listtransactions.php');
							break;
					}
					?>
					</td>
				</tr>
			</table>
		</td>
  </tr>
</table>
</div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php'); ?>